﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class MenuManager : BaseManager
    {
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="personnel_id"></param>
        /// <param name="level">获取菜单深度</param>
        /// <returns></returns>
        public static List<MenuInfo> GetMenu(string personnel_id, int level)
        {
            //创建菜单集合
            List<MenuInfo> menuList = new List<MenuInfo>();

            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable result = service.getpowerlist(personnel_id);
            DataTable dt = new DataTable();
            if (result.S)
            {
                dt = result.D;
                SysLoginUser loginUser = LoginManager.GetLoginUser();
                if (loginUser.IsWinform == 1)
                {
                    var d = dt.AsEnumerable().Where(x => (DataConvert.StrToInt(x["DisplayStyle"]) == 0 || DataConvert.StrToInt(x["DisplayStyle"]) == 2)).OrderBy(x => x["Sort"]);
                    if (d.Count() > 0)
                    {
                        dt = d.CopyToDataTable<DataRow>();
                    }
                    else
                    {
                        dt.Rows.Clear();
                    }
                }
                else
                {
                    var d = dt.AsEnumerable().Where(x => (DataConvert.StrToInt(x["DisplayStyle"]) == 1 || DataConvert.StrToInt(x["DisplayStyle"]) == 2)).OrderBy(x => x["Sort"]);
                    if (d.Count() > 0)
                    {
                        dt = d.CopyToDataTable<DataRow>();
                    }
                    else
                    {
                        dt.Rows.Clear();
                    }
                }
            }

            //获取一级菜单
            DataRow[] drList = dt.Select("ParentId = 0 and IsVisible = 0");
            foreach (var item in drList)
            {
                MenuInfo menuInfo = new MenuInfo();
                menuInfo.Id = item["Id"].ToString();
                menuInfo.Name = item["MenuName"].ToString();
                menuInfo.MenuId = item["MenuId"].ToString();
                menuInfo.ImgUrl = item["ImgUrl"].ToString();
                menuInfo.ParentId = item["ParentId"].ToString();
                menuInfo.IsVisible = item["IsVisible"].ToString();
                menuInfo.Sort = item["Sort"].ToString();
                menuList.Add(menuInfo);

                if (level == 1) continue;

                List<SecondLevelMenu> sMenuList = new List<SecondLevelMenu>();

                //获取二级菜单
                DataRow[] drList1 = dt.Select("ParentId = " + item["MenuId"].ToString() + " and IsVisible = 0");
                foreach (var item1 in drList1)
                {
                    SecondLevelMenu secondLevelMenu = new SecondLevelMenu();
                    secondLevelMenu.Id = item1["Id"].ToString();
                    secondLevelMenu.Name = item1["MenuName"].ToString();
                    secondLevelMenu.MenuId = item1["MenuId"].ToString();
                    secondLevelMenu.Path = item1["MenuPath"].ToString();
                    secondLevelMenu.ImgUrl = item1["ImgUrl"].ToString();
                    secondLevelMenu.ParentId = item1["ParentId"].ToString();
                    secondLevelMenu.IsVisible = item1["IsVisible"].ToString();
                    secondLevelMenu.Sort = item1["Sort"].ToString();
                    sMenuList.Add(secondLevelMenu);

                    if (level == 2) continue;

                    //获取三级菜单
                    DataRow[] drList2 = dt.Select("ParentId = " + item1["MenuId"].ToString() + " and IsVisible = 0");
                    if (drList2.Length > 0)
                    {
                        List<ThreeLevelMenu> tMenuList = new List<Model.ThreeLevelMenu>();

                        foreach (var item2 in drList2)
                        {
                            ThreeLevelMenu threeLevelMenu = new ThreeLevelMenu();
                            threeLevelMenu.Id = item2["Id"].ToString();
                            threeLevelMenu.Name = item2["MenuName"].ToString();
                            threeLevelMenu.MenuId = item2["MenuId"].ToString();
                            threeLevelMenu.Path = item2["MenuPath"].ToString();
                            threeLevelMenu.ImgUrl = item2["ImgUrl"].ToString();
                            threeLevelMenu.ParentId = item2["ParentId"].ToString();
                            threeLevelMenu.IsVisible = item2["IsVisible"].ToString();
                            threeLevelMenu.Sort = item2["Sort"].ToString();
                            tMenuList.Add(threeLevelMenu);
                        }
                        secondLevelMenu.children = tMenuList;
                    }
                }
                menuInfo.children = sMenuList;
            }

            return menuList;
        }

        /// <summary>
        /// 获取目录里的所有文件名
        /// </summary>
        /// <param name="strDirectory"></param>
        /// <returns></returns>
        public static string GetAllFilesNameInDirectory(string strDirectory)
        {
            var directory = new DirectoryInfo(strDirectory);
            FileInfo[] fileInfoArray = directory.GetFiles();

            string FileNameJson = "[";
            foreach (FileInfo fileinfo in fileInfoArray)
            {
                FileNameJson += "{ \"filename\":\"" + fileinfo.Name + "\"},";
            }
            FileNameJson = FileNameJson.TrimEnd(',');
            FileNameJson += "]";

            return FileNameJson;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public static OperateResult Save(tb_Menu menu)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_Menu result = new ResultHelperOftb_Menu();
                if (menu.ParentId == null) menu.ParentId = 0;
                if (menu.Id == 0)
                {
                    menu.AddTime = DateTime.Now;
                    menu.AddUserId = loginUser.Personnel_id;
                    menu.AddUserName = loginUser.Name;
                    if (string.IsNullOrEmpty(menu.ParentId.ToString()))
                    {
                        menu.ParentId = 0;
                    }
                    result = service.Addmenu(menu);
                }
                else
                {
                    result = service.updatemenu(menu);
                }
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult Delete(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Delmenu(id.ToString());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
